// shopMallPackage/pages/bindIngHouse/bindIngHouse.js
import { bindHouseRequest } from "../../../service/onetoone_service"
import debounce from "../../../tools/debounce"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      communityName: "",
      houseNumber: "",
      houseOwnerName: "",
      houseOwnerIdCard: "",
      address: "",
    },
    showCityArea: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //输入绑定
  inputCellChange(e) {
    let item = e.detail;
    let changeValue = `formData.${item.key}`
    this.setData({
      [changeValue]: item.value
    })
  },


  //绑定操作
  bindHouseAction() {
    console.log("JJLog:传递参数是:" + JSON.stringify(this.data.formData));
    let data = this.data.formData;
    if (!data.communityName.trim()) {
      wx.showToast({
        title: '请输入小区名称',
        icon: "none"
      })
      return;
    }
    if (!data.address) {
      wx.showToast({
        title: '请选择所在地区',
        icon: "none"
      })
      return;
    }
    if (!data.houseNumber.trim()) {
      wx.showToast({
        title: '请输入房号',
        icon: "none"
      })
      return;
    }
    if (!data.houseOwnerName.trim()) {
      wx.showToast({
        title: '请输入业主姓名',
        icon: "none"
      })
      return;
    }
    if (!data.houseOwnerIdCard.trim()) {
      wx.showToast({
        title: '请输入业主身份证号',
        icon: "none"
      })
      return;
    }
    debounce(this.bindHouseRequest)
  },
  bindHouseRequest() {
    wx.showLoading();
    bindHouseRequest(this.data.formData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        //  wx.showToast({
        //    title: '提交成功',
        //    icon:"success"
        //  })
        //  setTimeout(()=>{
        //    wx.navigateBack();
        //  },2000)

        wx.showModal({
          title: '提示',
          content: '提交房源成功，待审核通过后返还积分',
          showCancel: false,
          confirmColor: "#FF0000",
          success(res) {
            if (res.confirm) {
              wx.navigateBack();
            }
          }
        })
      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:绑定房源失败了:", err);
      wx.showToast({
        title: err || '操作失败',
        icon: "none",
        duration: 4000,
      })
    })
  },
  showSelectCityPop() {
    this.setData({
      showCityArea: true,
    })
  },
  cityPopHidden() {
    this.setData({
      showCityArea: false
    })
  },
  //确定选择区域
  cityPopConfirm(e) {
    // console.log("JJLog:城市返回是:" + JSON.stringify(e));
    let result = e.detail.result;
    // [{ "code": "510000", "name": "四川省" }, { "code": "510400", "name": "攀枝花市" }]
    let address = result[0].name + "-" + result[1].name + "-" + result[2].name
    this.setData({
      'formData.address': address,
      showCityArea: false,
    })
  },
})